{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Aim\n",
    "Get familiarized with the Blackjack Environment"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import sys\n",
    "if \"../\" not in sys.path:\n",
    "    sys.path.append(\"../\") \n",
    "\n",
    "from lib.envs.blackjack import BlackjackEnv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get the environment\n",
    "env = BlackjackEnv()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Number of episodes to run\n",
    "n_episodes = 20\n",
    "\n",
    "# Number of iterations to run\n",
    "n_iter = 100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "def print_observation(observation):\n",
    "    \"\"\"\n",
    "    Print the observation in an interpretable format.\n",
    "    \n",
    "    \"\"\"\n",
    "    score, dealer_score, usable_ace = observation\n",
    "    print('Player Score: {} (Usable ace: {}), Dealer Score: {}'.format(score, usable_ace, dealer_score))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "def strategy(observation):\n",
    "    \"\"\"\n",
    "    Decide the action to take for the given observation.\n",
    "    \n",
    "    \"\"\"\n",
    "    score, dealer_score, usable_ace = observation\n",
    "    \n",
    "    # Action 1: Show the next card\n",
    "    # Action 0: Stop showing cards\n",
    "    # If score >= 20, return action = 0, else action = 1\n",
    "    return 0 if score >= 20 else 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Player Score: 12 (Usable ace: False), Dealer Score: 10\n",
      "Taking action Hit\n",
      "Player Score: 14 (Usable ace: False), Dealer Score: 10\n",
      "Taking action Hit\n",
      "Player Score: 22 (Usable ace: False), Dealer Score: 10\n",
      "Game ended. Reward = -1.0 \n",
      "\n",
      "Player Score: 14 (Usable ace: False), Dealer Score: 4\n",
      "Taking action Hit\n",
      "Player Score: 24 (Usable ace: False), Dealer Score: 4\n",
      "Game ended. Reward = -1.0 \n",
      "\n",
      "Player Score: 16 (Usable ace: False), Dealer Score: 10\n",
      "Taking action Hit\n",
      "Player Score: 19 (Usable ace: False), Dealer Score: 10\n",
      "Taking action Hit\n",
      "Player Score: 22 (Usable ace: False), Dealer Score: 10\n",
      "Game ended. Reward = -1.0 \n",
      "\n",
      "Player Score: 19 (Usable ace: True), Dealer Score: 10\n",
      "Taking action Hit\n",
      "Player Score: 14 (Usable ace: False), Dealer Score: 10\n",
      "Taking action Hit\n",
      "Player Score: 21 (Usable ace: False), Dealer Score: 10\n",
      "Taking action Stick\n",
      "Player Score: 21 (Usable ace: False), Dealer Score: 10\n",
      "Game ended. Reward = 0.0 \n",
      "\n",
      "Player Score: 15 (Usable ace: False), Dealer Score: 7\n",
      "Taking action Hit\n",
      "Player Score: 20 (Usable ace: False), Dealer Score: 7\n",
      "Taking action Stick\n",
      "Player Score: 20 (Usable ace: False), Dealer Score: 7\n",
      "Game ended. Reward = 1.0 \n",
      "\n",
      "Player Score: 12 (Usable ace: False), Dealer Score: 10\n",
      "Taking action Hit\n",
      "Player Score: 19 (Usable ace: False), Dealer Score: 10\n",
      "Taking action Hit\n",
      "Player Score: 29 (Usable ace: False), Dealer Score: 10\n",
      "Game ended. Reward = -1.0 \n",
      "\n",
      "Player Score: 14 (Usable ace: False), Dealer Score: 10\n",
      "Taking action Hit\n",
      "Player Score: 24 (Usable ace: False), Dealer Score: 10\n",
      "Game ended. Reward = -1.0 \n",
      "\n",
      "Player Score: 20 (Usable ace: False), Dealer Score: 2\n",
      "Taking action Stick\n",
      "Player Score: 20 (Usable ace: False), Dealer Score: 2\n",
      "Game ended. Reward = -1.0 \n",
      "\n",
      "Player Score: 19 (Usable ace: False), Dealer Score: 2\n",
      "Taking action Hit\n",
      "Player Score: 29 (Usable ace: False), Dealer Score: 2\n",
      "Game ended. Reward = -1.0 \n",
      "\n",
      "Player Score: 12 (Usable ace: False), Dealer Score: 7\n",
      "Taking action Hit\n",
      "Player Score: 22 (Usable ace: False), Dealer Score: 7\n",
      "Game ended. Reward = -1.0 \n",
      "\n",
      "Player Score: 15 (Usable ace: False), Dealer Score: 10\n",
      "Taking action Hit\n",
      "Player Score: 21 (Usable ace: False), Dealer Score: 10\n",
      "Taking action Stick\n",
      "Player Score: 21 (Usable ace: False), Dealer Score: 10\n",
      "Game ended. Reward = 1.0 \n",
      "\n",
      "Player Score: 20 (Usable ace: False), Dealer Score: 8\n",
      "Taking action Stick\n",
      "Player Score: 20 (Usable ace: False), Dealer Score: 8\n",
      "Game ended. Reward = 1.0 \n",
      "\n",
      "Player Score: 14 (Usable ace: False), Dealer Score: 10\n",
      "Taking action Hit\n",
      "Player Score: 17 (Usable ace: False), Dealer Score: 10\n",
      "Taking action Hit\n",
      "Player Score: 20 (Usable ace: False), Dealer Score: 10\n",
      "Taking action Stick\n",
      "Player Score: 20 (Usable ace: False), Dealer Score: 10\n",
      "Game ended. Reward = 1.0 \n",
      "\n",
      "Player Score: 20 (Usable ace: True), Dealer Score: 9\n",
      "Taking action Stick\n",
      "Player Score: 20 (Usable ace: True), Dealer Score: 9\n",
      "Game ended. Reward = 1.0 \n",
      "\n",
      "Player Score: 18 (Usable ace: False), Dealer Score: 8\n",
      "Taking action Hit\n",
      "Player Score: 22 (Usable ace: False), Dealer Score: 8\n",
      "Game ended. Reward = -1.0 \n",
      "\n",
      "Player Score: 16 (Usable ace: False), Dealer Score: 10\n",
      "Taking action Hit\n",
      "Player Score: 26 (Usable ace: False), Dealer Score: 10\n",
      "Game ended. Reward = -1.0 \n",
      "\n",
      "Player Score: 13 (Usable ace: True), Dealer Score: 9\n",
      "Taking action Hit\n",
      "Player Score: 20 (Usable ace: True), Dealer Score: 9\n",
      "Taking action Stick\n",
      "Player Score: 20 (Usable ace: True), Dealer Score: 9\n",
      "Game ended. Reward = 1.0 \n",
      "\n",
      "Player Score: 12 (Usable ace: False), Dealer Score: 9\n",
      "Taking action Hit\n",
      "Player Score: 16 (Usable ace: False), Dealer Score: 9\n",
      "Taking action Hit\n",
      "Player Score: 20 (Usable ace: False), Dealer Score: 9\n",
      "Taking action Stick\n",
      "Player Score: 20 (Usable ace: False), Dealer Score: 9\n",
      "Game ended. Reward = 0.0 \n",
      "\n",
      "Player Score: 20 (Usable ace: True), Dealer Score: 1\n",
      "Taking action Stick\n",
      "Player Score: 20 (Usable ace: True), Dealer Score: 1\n",
      "Game ended. Reward = -1.0 \n",
      "\n",
      "Player Score: 18 (Usable ace: False), Dealer Score: 7\n",
      "Taking action Hit\n",
      "Player Score: 22 (Usable ace: False), Dealer Score: 7\n",
      "Game ended. Reward = -1.0 \n",
      "\n"
     ]
    }
   ],
   "source": [
    "for i in range(n_episodes):\n",
    "    observation = env.reset()\n",
    "    \n",
    "    for it in range(n_iter):\n",
    "        \n",
    "        print_observation(observation)\n",
    "        action = strategy(observation)\n",
    "        print(\"Taking action {}\".format([\"Stick\", \"Hit\"][action]))\n",
    "        observation, reward, done, _ = env.step(action)\n",
    "        \n",
    "        # Termination\n",
    "        if done:\n",
    "            print_observation(observation)\n",
    "            print('Game ended. Reward = {} \\n'.format(float(reward)))\n",
    "            break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
